package com.ouyu.tech.ifelsereplace.test;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Set;

@ConditionalOnProperty(prefix = "condition.enum",value ="mark")
@Component
@Configuration
public class ConditionBeanPost implements BeanPostProcessor {
    @Autowired
    DataEnum dataEnum;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        Set<Map.Entry<String, String>> data = dataEnum.getCondition().entrySet();
        for(Map.Entry<String, String> map:data){
            if(beanName.equals(map.getValue())){
                LoadEnumData.getSingleInstance().setInstanceLoad(map.getKey(),(Condition) bean);
            }
        }

       /* if(bean.getClass().isAnnotation()){
            Annotation[] annitation = bean.getClass().getAnnotations();
            for(int i=0;i<annitation.length;i++){
                if(annitation[i].equals(LoadEnum.class)){

                    LoadEnumData.getSingleInstance().setInstanceLoad();
                }
            }
        }*/
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
